var currentTreeNode = null;
var treeObj;
var minForm;

var dictionaryList=new dataGrid("wfDictionaryP");
var dictionaryInfoList=new dataGrid("wfDictionaryC");
var frameId = window.frameElement && window.frameElement.id || '';
var dicValue = RequestURLParam.getParam("dicValue");

$(document).ready(function () {
    if(dicValue != "undefined"){
        dictionaryList.load({}, "dictionaryId", dicValue);
    }else{
        dictionaryList.load({});
    }
    dictionaryInfoList.load({"parentId" : "false"})
});

var pid;
var enName;
function rootClick(e){
    pid=e.dictionaryId;
    enName=e.enName;
    cnName=e.cnName;
    recGrid(pid)
}

function repGrid() {
    dictionaryList.reload({},1,dicValue);
}

function recGrid(pid) {
    ActHttpRequest("/wf/dictionary/findById",{"id":pid},function (data) {
        var wd = data.wfDictionary;
        $("#parentName").html("-"+wd.cnName);
    });
    dictionaryInfoList.reload({"parentId" : pid});
}

function codeRootEdit(e){
    var row=e.record;
    return "<a class='layui-btn layui-btn-xs st_back1' lay-event='edit' onClick='editParent(\""+e.value+"\")'><i class='layui-icon'>&#xe642;</i></a>" +
    "<a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onClick='removeData(\""+e.value+"\")'><i class='layui-icon'>&#xe640;</i></a>";
}

function codeRootEdit2(e){
    var row=e.record;
    return "<a class='layui-btn layui-btn-xs st_back1' lay-event='edit' onClick='editChild(\""+e.value+"\")'><i class='layui-icon'>&#xe642;</i></a>" +
        "<a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onClick='removeData(\""+e.value+"\")'><i class='layui-icon'>&#xe640;</i></a>";
}

//编辑
function editParent(id) {
    parent.layer.open({
        type: 2
        ,title: '编辑类型'
        ,area: ['500px', '450px']
        ,maxmin: false
        ,content: '../froala/wfDictionaryParentDetail.html?type=edit&id='+id+'&iframeId='+frameId
        ,zIndex: layer.zIndex
    });
}

function editChild(id) {
    parent.layer.open({
        type: 2
        ,title: '编辑类型'
        ,area: ['500px', '450px']
        ,maxmin: false
        ,content: '../froala/wfDictionaryChildDetail.html?type=edit&id='+id+'&parentId='+pid+"&enName="+enName+"&iframeId="+frameId
        ,zIndex: layer.zIndex
    });
}

//删除
function removeData(id){
    layer.confirm('确定要删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        ActHttpRequest("/wf/dictionary/del", {"id":id}, function (data) {
            if (data.OK == "OK") {
                repGrid();//加载根目录类型
                recGrid(pid);//加载根目录类型
                layer.msg("删除成功！",{icon : 1 , time : 800});
            } else {
                layer.alert(data.message);
            }
        });
    });
}

//添加数据字典
function addParent(){
    parent.layer.open({
        type: 2
        ,title: '添加字典'
        ,area: ['500px', '450px']
        ,maxmin: false
        ,content: '../froala/wfDictionaryParentDetail.html?type=add&iframeId='+frameId
        ,zIndex: layer.zIndex
    });
}

//添加字典内容
function addChild(){
    if(pid && pid != ""){
        parent.layer.open({
            type: 2
            ,title: '添加字典'
            ,area: ['500px', '450px']
            ,maxmin: false
            ,content: '../froala/wfDictionaryChildDetail.html?type=add&parentId='+pid+"&enName="+enName+"&iframeId="+frameId
            ,zIndex: layer.zIndex
        });
    }else{
        parent.layer.msg("请选择数据字典",{icon : 5,time : 800});
    }
}

//确定
function save() {
    var index = parent.layer.getFrameIndex(window.name);
    var iframe = RequestURLParam.getParam("iframeId");
    node = $('#'+iframe,parent.document)[0].contentWindow.node;
    var dictionary = JSON.parse(dictionaryList.getCheckData());
    var id = dictionary[0].dictionaryId;
    node.attr('dic-value', id)
    parent.layer.close(index);
}
